package com.zrf.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.Query;
import org.hibernate.Session;

import com.session.factory.HibernateSessionFactory;
import com.zrf.pojo.User;

@Path("user")
public class UserService {

	@GET
	@Path("login/{ubankcard},{uloginpassword}")
	@Produces(MediaType.APPLICATION_JSON)
	public String login(@PathParam("ubankcard") String ubc,@PathParam("uloginpassword") String ulp){
		int state = 1;
		try {
			Session session = HibernateSessionFactory.getSession();
			String hql = "from User u where u.ubankcard=?";
			Query query = session.createQuery(hql);
			query.setString(0, ubc);
			User u = (User) query.uniqueResult();
			try {
				if(ulp.equals(u.getUloginpassword())){
					return "{\"login\":[{\"state\":\""+state+"\"}]}";
				}
				state = 0;
				return "{\"login\":[{\"state\":\""+state+"\"}]}";
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			
		}
		HibernateSessionFactory.closeSession();
		return "{\"login\":[{\"state\":\"0\"}]}";
	}
}
